import 'package:logger/logger.dart';

final logger = Logger(printer: PrettyPrinter());

void main(List<String> arguments) {
  Future.delayed(const Duration(seconds: 2), () {
    throw AssertionError("Error");
  }).then((value) {
    //执行成功会走到这里
    logger.i(value);
  }).catchError((e) {
    //执行失败会走到这里
    logger.i(e);
  }).whenComplete(() {
    logger.i("关闭任务！");
  });

  logger.i("--------------------------");

  Future.wait([
    Future.delayed(const Duration(seconds: 1), () {
      return "hello";
    }),
    Future.delayed(const Duration(seconds: 2), () {
      return " world";
    }),
  ]).then((values) {
    logger.i(values[0] + values[1]);
  }).catchError((e) {
    logger.i(e);
  });

  logger.i("--------------------------");
  task();

  Stream.fromFutures([
    // 1秒后返回结果
    Future.delayed(const Duration(seconds: 1), () {
      return "hello 1";
    }),
    // 抛出一个异常
    Future.delayed(const Duration(seconds: 2), () {
      throw AssertionError("Error");
    }),
    // 3秒后返回结果
    Future.delayed(const Duration(seconds: 3), () {
      return "hello 3";
    })
  ]).listen((event) {
    logger.i('Stream listen $event');
  }, onError: (e) {
    logger.i('Stream onError $e');
  }, onDone: () {
    logger.i("任务完成");
  });
}

task() async {
  try {
    String userId = await login("admin", "123456");
    String info = await getUserInfo(userId);
    await saveUserInfo(info);
  } catch (e) {
    logger.i(e);
  }
}

Future<String> login(String username, String password) async {
  return "$username , $password , 0001";
}

Future<String> getUserInfo(String userId) async {
  return "$userId , 李四";
}

Future<void> saveUserInfo(String info) async {
  logger.i(info);
}
